package com.linjiahao.articleService.controller;


import com.linjiahao.articleService.entity.Article;
import com.linjiahao.articleService.service.ArticleService;
import com.linjiahao.commonutils.JwtUtils;
import com.linjiahao.commonutils.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-13
 */
@RestController
@RequestMapping("/articleService/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @PostMapping("create")
    public ResultMap createArticle(@RequestBody Article article, HttpServletRequest request){
        String memberID= JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(article);
        Boolean aBoolean = articleService.insetArticle(article, memberID);
        if(aBoolean){
            return ResultMap.ok().message("创建文章成功");
        }
        return ResultMap.error().message("创建文章失败");
    }

    @GetMapping("getAll")
    public ResultMap getAllaritcile(HttpServletRequest request){
        String memberId=JwtUtils.getMemberIdByJwtToken(request);
        List<Article> allarticle = articleService.getAllarticle(memberId);
        return ResultMap.ok().data("ariticle",allarticle);
    }

    @GetMapping("addVistist/{id}")
    public ResultMap addVitist(@PathVariable String id){
        Integer vitsi = articleService.addVitsi(id);
        return ResultMap.ok().data("vitsi",vitsi);
    }

}

